package org.batatasframework.support.database.sql.field;

import java.util.List;

import org.batatasframework.support.database.FieldFormatable;
import org.batatasframework.support.database.metadatas.FieldMetadata;
import org.batatasframework.support.database.sql.TableRuntimeMaster;
import org.batatasframework.support.database.sql.sort.SortFragment;
import org.batatasframework.support.database.sql.sort.SortType;
import org.batatasframework.support.database.values.FieldValueHolder;
import org.batatasframework.support.database.values.OptionsValueHolder;
import org.batatasframework.support.format.OptionalFormat;

import cn.bonoon.kernel.util.StringHelper;

/**
 * <pre>在直接使用查询数据库的时候，如果某个字段需要进行格式化处理的，
 * 可以使用{@link #setFormat(Object)}进行指定，支持的格式包括：</pre>
 * <p><ul>
 * <li>{@link java.text.DecimalFormat DecimalFormat}
 * <li>{@link java.text.DateFormat DateFormat}
 * <li>{@link org.batatasframework.support.format.BooleanFormat BooleanFormat}
 * <li>{@link org.batatasframework.support.format.OptionalFormat OptionalFormat}
 * <li>{@link org.batatasframework.support.format.StringLimitsFormat StringLimitsFormat}
 * <li>其它格式...
 * </ul></p>
 * 
 * @author jackson
 * @see FieldFormatable
 */
public abstract class AbstractSelectFragment implements SelectFragment, FieldFormatable{
	
	/**
	 * <pre>
	 * 为什么使用单独的对象，因为查询某一个字段可能在查询语句里多次出现；
	 * 当多次出现的时候，查询语句里应该只查询一次这个字段，否则会多次返回这个字段的数据。
	 * </pre>
	 */
	private final FieldElement fieldElement;
	private final TableRuntimeMaster master;
	//在定义的过程中，是可以重新指定格式化的类型的
	private Object format;
	
	protected String title;
	
	public AbstractSelectFragment(TableRuntimeMaster master, FieldElement fieldElement){
		this.master = master;
		this.fieldElement = fieldElement;
		
		this.title = fieldElement.getTitle();
		this.format = fieldElement.getFormat();
	}
	
	@Override
	public FieldValueHolder getValueHolder() {
		
		String alias = fieldElement.getAlias();
		
		if(format instanceof OptionalFormat){
			return new OptionsValueHolder(this, alias, (OptionalFormat)format);
		}
		
		return fieldElement.getDataType().buildValueHolder(this, alias, format);
	}
	
	/**
	 * 把这一个字段当作一个排序的字段
	 * @return
	 */
	public SortFragment sort(){
		return sort(SortType.NULL, 0);
	}
	
	public SortFragment sort(SortType sortType, int ordinal){
		return master.orderBy(fieldElement, sortType, ordinal);
	}
	
	@Override
	public FieldMetadata find(String name) {
		if(name.equals(fieldElement.getAlias())) return fieldElement;
		return null;
	}

	@Override
	public void readSource(List<FieldMetadata> fields) {
		if(!fields.contains(fieldElement)){
			fields.add(fieldElement);
		}
	}
	
	@Override
	public String getTitle() {
		return StringHelper.get(title, fieldElement.getAlias());
	}
	
	public SelectFragment setTitle(String title) {
		if(StringHelper.isNotEmpty(title)){
			this.title = title;
		}
		return this;
	}
	
	/** 参考：{@link FieldFormatable} */
	public SelectFragment setFormat(Object format) {
		if(null != format){
			this.format = format;
		}
		return this;
	}
	
	@Override
	public Object getFormat() {
		return format;
	}
	
	private String[] group;
	
	@Override
	public String[] getGroup() {
		return group;
	}
	
	public SelectFragment setGroup(String[] group) {
		if(null != group && group.length > 0){
			this.group = group;
		}
		return this;
	}
}
